<?php

Yii::import('common.models._base.BaseTrainerFollow');

class TrainerFollow extends BaseTrainerFollow {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public static function saveTrainerfollowersafterlogin() {
        $userid = getLoginUserId();
        $trainerfollerdata = Yii::app()->session['trainer_follower'];
        $followers = getEmailbyuserId('Users', $userid);
        $followers =$followers->username;
        if (isset($trainerfollerdata['type']) && $trainerfollerdata['type'] == 'course') {
            $classDetails = Courses::model()->findByPk($trainerfollerdata['class_id']);
            $authKey = $classDetails->auth_key;
            $profiles = getUserTypeByClassCourse('Courses', $trainerfollerdata['class_id']);
            $trainername = $profiles->first_name . ' ' . $profiles->last_name;
        } else {
            $classDetails = Classes::model()->findByPk($trainerfollerdata['class_id']);
            $authKey = $classDetails->auth_key;
            $profiles = getUserTypeByClassCourse('Classes', $trainerfollerdata['class_id']);
            $trainername = $profiles->first_name . ' ' . $profiles->last_name;
        }
        $model = new TrainerFollow();
        if (!empty($classDetails->backend_user_id)) {
            $classUserId = '';
            $mailTousers = getEmailbyuserId('BackendUser', $classDetails->backend_user_id);
            $mailTouser = $mailTousers->email;
            $model->setAttribute('trainer_backend_user_id', $classDetails->backend_user_id);
        } else {
            $mailTousers = getEmailbyuserId('Users', $classDetails->users_id);
            $mailTouser = $mailTousers->username;
            $classUserId = $classDetails->users_id;
            $model->setAttribute('trainer_front_end_user_id', $classDetails->users_id);
        }
        $model->setAttribute('folower_user_id', $userid);
        $model->setAttribute('create_time', SkillConfig::CreateDateTime());
        $model->setAttribute('status', SkillConfig::STATUS_ACTIVE);
        $criteria = new CDbCriteria;
        $criteria->condition = 'folower_user_id=' . $userid . ' AND (trainer_front_end_user_id=' . $mailTousers->id . '  OR trainer_backend_user_id=' . $mailTousers->id . ')';
        $alredayFolow = TrainerFollow::model()->find($criteria);
        if (!empty($alredayFolow) && $alredayFolow->folower_user_id) {
            $errorCode = Message::repeat_user;
            unset(Yii::app()->session['trainer_follower']);
        } else {
            if ($userid != $classUserId) {
                $model->save();
                unset(Yii::app()->session['trainer_follower']);
                Yii::app()->runController('TrainerFollow/sendmailtotrainerfollwers/trainername/' . $trainername . '/followername/' . $followers . '/mailto/' . $mailTouser);
                $errorCode = Message::success;
            } else {
                $errorCode = Message::owan_user;
            }
        }
        if ($errorCode == Message::success) {
            return Yii::app()->createAbsoluteUrl('users/trainerProfile', array('authKey' => $authKey));
        } else {
            return Yii::app()->createAbsoluteUrl('trainerFollow/trainerfolwers', array('skillcode' => $errorCode));
        }
    }

}